Skip to content

feat: allow fixing the vertical order of Sankey nodes#7873

Open
adamreeve wants to merge 5 commits into
plotly:v4.0from
adamreeve:sankey-order
Open

feat: allow fixing the vertical order of Sankey nodes#7873
adamreeve wants to merge 5 commits into
plotly:v4.0from
adamreeve:sankey-order

Conversation

@adamreeve

@adamreeve adamreeve commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

Fixes #4373

Follow up to #7830

This exposes the nodeSort method added to d3-sankey, to allow disabling the default ordering algorithm and enforcing that the vertical order should match the order of nodes in the input data.

Note that the nodeSort method in d3-sankey can be a method that compared two nodes (see the docs), but for many users I think just providing a way to pass null instead of the default undefined is sufficient. But maybe this could be future-proofed somehow to support using a comparison method in future? Although I'm not sure how feasible it is to make that work in plotly.

The nodeSort option is not supported by d3-sankey-circular, so is not used for circular Sankey diagrams (see #7689 (comment) for more context).

@adamreeve adamreeve marked this pull request as ready for review June 29, 2026 02:16

@camdecoster camdecoster left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems great. I've requested a few small changes. What do you think of adding support for link sort?

right: d3Sankey.sankeyRight,
center: d3Sankey.sankeyCenter
}[trace.node.align];
var input_sort = trace.node.sort === 'input';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var input_sort = trace.node.sort === 'input';
const inputNodeSort = trace.node.sort === 'input';

Comment on lines +52 to +55
if(circular && input_sort) {
Lib.error('Circular Sankey diagrams do not support the "input" node.sort mode; falling back to the default sort.');
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if(circular && input_sort) {
Lib.error('Circular Sankey diagrams do not support the "input" node.sort mode; falling back to the default sort.');
}

// d3-sankey-circular does not support the nodeSort method
if(!circular) {
sankey.nodeSort(input_sort ? null : undefined);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
if (inputNodeSort) {
if (circular) {
Lib.warn('Circular Sankey diagrams do not support the "input" node.sort mode; falling back to the default sort.');
} else {
// Passing null keeps nodes in their input order
sankey.nodeSort(null);
}
}

Comment on lines +178 to +180
'If the value is `auto` (the default), the vertical order of nodes will be determined automatically',
'by the layout.',
'If the value is `input`, the vertical order is kept the same as the order in the input node array'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'If the value is `auto` (the default), the vertical order of nodes will be determined automatically',
'by the layout.',
'If the value is `input`, the vertical order is kept the same as the order in the input node array'
'For `auto` (the default), the vertical order of nodes will be determined automatically by the layout.',
'For `input`, the vertical order of nodes is kept the same as the order in the input array.'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this mock can be deleted since all of the other sankey mocks use the auto sort.

}
],
"layout": {
"title": { "text": "Sankey with fixed vertical node ordering" },

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"title": { "text": "Sankey with fixed vertical node ordering" },
"title": { "text": "Sankey with vertical node ordering matching the input array" },

Comment on lines +505 to +508
var errors = [];
spyOn(Lib, 'error').and.callFake(function (msg) {
errors.push(msg);
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var errors = [];
spyOn(Lib, 'error').and.callFake(function (msg) {
errors.push(msg);
});
var warnings = [];
spyOn(Lib, 'warn').and.callFake(function (msg) {
warnings.push(msg);
});

expect(d3SelectAll('.sankey .node-rect').size()).toBeGreaterThan(0);

// An error is logged about the fallback
expect(errors.length).toBe(1);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
expect(errors.length).toBe(1);
expect(warnings.length).toBe(1);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants